package org.stwy.lease.contract;

import java.util.Date;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stwy.comm.base.BaseRecordsController;
import org.stwy.pro.asset.RecordEntity;

import com.rap.common.exception.ServiceException;
import com.rap.frame.web.ActionResultModel;
import com.rap.frame.web.BaseController;

/*
 * 合同 控制类
 */
/**
*版权信息 (c) RAP 保留所有权利.
*@author Rap.Codegen
*@Team RAP Team
*@Date 2015-4-17 20:48:15
*<p></p>
*
*@参与人：Rap.Codegen
*/

@Controller
@RequestMapping(value = "/lease/contract")
public class ContractController extends BaseRecordsController<ContractEntity> {

	public ContractController() {
		setResourceIdentity("stwy:lease:contract");
	}
	private ContractService getService(){
		return (ContractService)super.baseService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String view(Model model) {
		super.assertHasViewPermission();
		return "stwy/lease/contract/contract_main";
	}
	@RequestMapping(value = "/view")
	public String contractView(Model model) {
		return "stwy/lease/contract/contract_view";
	}
	@RequestMapping(value = "/select")
	public String select(Model model, String callback) {
		model.addAttribute("callback", callback);
		return "stwy/lease/contract/contract_select";
	}
	@RequestMapping(value = "/hasdeleted")
	public String hasdeleted(Model model) {
		return "stwy/lease/contract/contract_hasdeleted";
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/addContract")
	@ResponseBody
	public ActionResultModel addContract(ServletRequest request, Model model,
			@Valid ContractEntity entity,@RequestParam(value = "assetIds[]",required=true)String[] assetIds) {
		ActionResultModel arm = new ActionResultModel();
		try {
			entity = getService().add(entity,assetIds);
			arm.setRecords(entity);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/updateContract")
	@ResponseBody
	public ActionResultModel updateContract(ServletRequest request, Model model,
			@Valid ContractEntity entity,@RequestParam(value = "assetIds[]",required=true)String[] assetIds) {
		ActionResultModel arm = new ActionResultModel();
		try {
			entity = getService().updateContract(entity,assetIds);
			arm.setRecords(entity);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	/**
	 * 退租
	 * @param request
	 * @param model
	 * @param recordDate
	 * @param reason
	 * @param pks
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/quit")
	@ResponseBody
	public ActionResultModel quit(ServletRequest request, Model model
			,Date recordDate,Double history,Double deposit,String reason,String[] pks) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().doQuit(pks, recordDate, reason,history,deposit);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	/**
	 * 解除合同
	 * @param request
	 * @param model
	 * @param recordDate
	 * @param history
	 * @param deposit
	 * @param reason
	 * @param pks
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public ActionResultModel cancel(ServletRequest request, Model model,
			Date recordDate,String reason,String[] pks) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().doCancel(pks, recordDate, reason);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/renew")
	@ResponseBody
	public ActionResultModel renew(ServletRequest request, Model model
			,ContractEntity renewContract) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().doRenew(renewContract);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/genContractCode")
	@ResponseBody
	public ActionResultModel genContractCode() {
		ActionResultModel arm = new ActionResultModel();
		try {
			String code = getService().genContractCode();
			arm.setRecords(code);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	
}
